package com.ruoyi.organization.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.organization.mapper.OrganizationMapper;
import com.ruoyi.organization.domain.Organization;
import com.ruoyi.organization.service.IOrganizationService;

/**
 * 机构信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-11
 */
@Service
public class OrganizationServiceImpl implements IOrganizationService 
{
    @Autowired
    private OrganizationMapper organizationMapper;

    /**
     * 查询机构信息
     * 
     * @param organizationId 机构信息主键
     * @return 机构信息
     */
    @Override
    public Organization selectOrganizationByOrganizationId(Long organizationId)
    {
        return organizationMapper.selectOrganizationByOrganizationId(organizationId);
    }

    /**
     * 查询机构信息列表
     * 
     * @param organization 机构信息
     * @return 机构信息
     */
    @Override
    public List<Organization> selectOrganizationList(Organization organization)
    {
        return organizationMapper.selectOrganizationList(organization);
    }

    /**
     * 新增机构信息
     * 
     * @param organization 机构信息
     * @return 结果
     */
    @Override
    public int insertOrganization(Organization organization)
    {
        organization.setCreateTime(DateUtils.getNowDate());
        return organizationMapper.insertOrganization(organization);
    }

    /**
     * 修改机构信息
     * 
     * @param organization 机构信息
     * @return 结果
     */
    @Override
    public int updateOrganization(Organization organization)
    {
        organization.setUpdateTime(DateUtils.getNowDate());
        return organizationMapper.updateOrganization(organization);
    }

    /**
     * 批量删除机构信息
     * 
     * @param organizationIds 需要删除的机构信息主键
     * @return 结果
     */
    @Override
    public int deleteOrganizationByOrganizationIds(Long[] organizationIds)
    {
        return organizationMapper.deleteOrganizationByOrganizationIds(organizationIds);
    }

    /**
     * 删除机构信息信息
     * 
     * @param organizationId 机构信息主键
     * @return 结果
     */
    @Override
    public int deleteOrganizationByOrganizationId(Long organizationId)
    {
        return organizationMapper.deleteOrganizationByOrganizationId(organizationId);
    }

    /**
     * 根据机构名称查询机构ID
     *
     * @param orgName 机构名称
     * @return 机构ID
     */
    @Override
    public Organization selectOrgIdByName(String orgName) {

        return organizationMapper.selectOrgIdByName(orgName);
    }

    /**
     * 构建机构树
     *
     * @return 结果
     */
    @Override
    public List<Organization> buildOrgTree() {
        // 查询所有未删除的组织
        Organization queryOrg = new Organization();
        queryOrg.setDelFlag("0"); // 假设 0 表示未删除
        // 查询所有组织
        List<Organization> allOrgs = organizationMapper.selectOrganizationList(queryOrg);

        // 构建 Map 用于快速查找
        Map<Long, Organization> orgMap = allOrgs.stream()
                .collect(Collectors.toMap(Organization::getOrganizationId, o -> o));

        // 构建树形结构
        List<Organization> rootOrgs = new ArrayList<>();
        for (Organization org : allOrgs) {
            Long parentId = org.getParentId();
            if (parentId == 0) {
                rootOrgs.add(org);
            } else {
                Organization parent = orgMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildrenCode() == null) {
                        parent.setChildrenCode(new ArrayList<>());
                    }
                    parent.getChildrenCode().add(org);
                }
            }
        }

        return rootOrgs;
    }
}
